package com.toman.ui.work.workshop;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.toman.Interfaces.NewBaseUrl;
import com.toman.JavaBean.CombineEntity;
import com.toman.R;
import com.toman.Util.JsonUtils;
import com.toman.Util.ToastUtil;
import com.toman.Util.ViewUtil;
import com.toman.toolbar.ToolBarActivity;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Request;
import okhttp3.Response;

public class SubmitMoreChcekActivity extends ToolBarActivity {
    CombineEntity bean;
    @BindView(R.id.tv_completed_count)
    TextView tvCompletedCount;
    @BindView(R.id.tv_qualified_count)
    TextView tvQualifiedCount;
    @BindView(R.id.tv_scrapped_count)
    TextView tvScrappedCount;
    @BindView(R.id.tv_waste_count)
    TextView tvWasteCount;
    @BindView(R.id.tv_remake_count)
    TextView tvRemakeCount;
    @BindView(R.id.tv_reuse_count)
    TextView tvReuseCount;
    @BindView(R.id.et_completed_count)
    EditText etCompletedCount;
    @BindView(R.id.et_qualified_count)
    EditText etQualifiedCount;
    @BindView(R.id.et_scrapped_count)
    EditText etScrappedCount;
    @BindView(R.id.et_waste_count)
    EditText etWasteCount;
    @BindView(R.id.et_remake_count)
    EditText etRemakeCount;
    @BindView(R.id.et_reuse_count)
    EditText etReuseCount;
    @BindView(R.id.tv_order_remark_name)
    TextView tvOrderRemarkName;
    @BindView(R.id.et_order_remark)
    EditText etOrderRemark;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_submit_more_chcek);
        ButterKnife.bind(this);
        initToolBar("量产报工审核");
        initIntent();
        initView();
    }

    private void initView() {
        tvCompletedCount.setText(bean.getCounts12().getCompletedCount());
        tvQualifiedCount.setText(bean.getCounts12().getQualifiedCount());
        tvRemakeCount.setText(bean.getCounts12().getRemakeCount());
        tvReuseCount.setText(bean.getCounts12().getReuseCount());
        tvScrappedCount.setText(bean.getCounts12().getScrappedCount());
        tvWasteCount.setText(bean.getCounts12().getWasteCount());
        String[] str = new String[]{bean.getCounts12().getCompletedCount(), bean.getCounts12().getQualifiedCount(), bean.getCounts12().getRemakeCount(), bean.getCounts12().getReuseCount(), bean.getCounts12().getScrappedCount(), bean.getCounts12().getWasteCount()};
        ViewUtil.setText(str, etCompletedCount, etQualifiedCount, etRemakeCount, etReuseCount, etScrappedCount, etWasteCount);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.toolbar_finish, menu);

        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.menu_finish:
                        add();
                        break;
                }
                return false;
            }
        });

        return true;
    }

    private void initIntent() {
        bean = (CombineEntity) getIntent().getSerializableExtra("bean");
    }

    public void add() {
        String[] item = new String[]{"完工数量", "合格数量", "报废数量", "料废数量", "返工数量", "回用数量"};//直接初始化

        if (ViewUtil.isNull(this, item, etCompletedCount, etQualifiedCount, etScrappedCount, etWasteCount, etRemakeCount, etReuseCount)
                ) {
            return;
        }
        if ( ViewUtil.compareString(this,item,etCompletedCount,etQualifiedCount)
                ) {
            return;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", bean.getId() + "");
        map.put("verifyCompletedCount", etCompletedCount.getText().toString());
        map.put("verifyQualifiedCount", etQualifiedCount.getText().toString());
        map.put("verifyScrappedCount", etScrappedCount.getText().toString());
        map.put("verifyWasteCount", etWasteCount.getText().toString());
        map.put("verifyRemakeCount", etRemakeCount.getText().toString());
        map.put("verifyReuseCount", etReuseCount.getText().toString());


        String str = JsonUtils.obj2JsonString(map);
        OkHttpUtils
                .post(NewBaseUrl.workshop_submit_benchreport_verify)
                .headers("token", token)
                .headers("Content-Type", "application/json")
                .postJson(str)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", getClass().toString() + result);
                        JSONObject jsonObj = JSON.parseObject(result);
                        int code = jsonObj.getInteger("code");ViewUtil.lagout(code,getApplicationContext());

                        if (code == 0) {
                            ToastUtil.makeText(getApplicationContext(), jsonObj.getString("msg"));
                            finish();

                        } else {
                            ToastUtil.error(getApplicationContext(), jsonObj.getString("msg"));
                        }

                    }
                });

    }


}
